<?php

// +----------------------------------------------------------------------
// | 行为日志模型
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\model\ucenter;

use app\model\app\Modular;
use think\facade\Db;

class UserMoneyLog extends Common
{

    public static $money_type = [
        '1' => '余额提现',
        '2' => '提现驳回',
        '6' => '充值余额',
        '11' => '直推奖',
        '19' => '间接奖',
        '12' => '平级奖',
        '13' => '级差奖',
        '14' => '隔代奖',
        '17' => '培养奖',
        '15' => '一级邀请奖励',
        '16' => '二级邀请奖励',
        '18' => '推广一名合伙人',
        '20' => '推广团队奖励',
        '21' => '推广合伙人奖励',
        '30' => '发布任务',
        '31' => '缴纳竞拍保证金',
        '32' => '支付推荐费用',
        '33' => '竞拍成功',
        '34' => '竞拍违约',
        '35' => '任务变动',
        '36' => '发布手机接单退回',
        '37' => '游戏试玩',
        '38' => '卡券专区',
        '39' => '电商返利',
        '50' => '积分兑换',
        '51' => '生活缴费',
        '52' => 'APP推广',
        '53' => '出行返利',
        '66' => '服务商收益',
        '88' => '财务管理',
        '89' => '新人红包',
        '99' => '财务冲正',
//            -----以上旧版程序---
        '22' => '间推奖',
        '90' => 'POS交易返佣',
        '91' => '流量返现（专项）',
        '92' => '交易返现（专项）',
        '95' => '专属利差',
        '93' => '升级押金'
//                '90'=> 'POS交易返佣',
//                '91'=> '流量返现（专项）',
//                '92'=> '交易返现（专项）',
//                '93'=> '升级押金'
    ];

    public static $show_type = [
        '1' => '余额提现',
        '2' => '提现驳回',
        '6' => '充值余额',
        '11' => '展业收益',
        '19' => '邀请奖励',
        '12' => '邀请奖励',
        '13' => '邀请奖励',
        '14' => '管理奖励',
        '17' => '管理奖励',
        '15' => '邀请奖励',
        '16' => '邀请奖励',
        '18' => '邀请奖励',
        '20' => '邀请奖励',
        '21' => '邀请奖励',
        '30' => '发布任务',
        '31' => '缴纳竞拍保证金',
        '32' => '支付推荐费用',
        '33' => '竞拍成功',
        '34' => '竞拍违约',
        '35' => '任务变动',
        '36' => '发布手机接单退回',
        '37' => '展业收益',
        '38' => '展业收益',
        '39' => '展业收益',
        '50' => '展业收益',
        '51' => '展业收益',
        '52' => '展业收益',
        '53' => '展业收益',
        '66' => '服务商收益',
        '88' => '财务管理',
        '89' => '新人红包',
        '99' => '财务管理',
//            -----以上旧版程序---
        '22' => '管理奖励',
        '90' => '展业收益',
        '91' => '流量返现（专项）',
        '92' => '交易返现（专项）',
        '95' => '邀请奖励',
        '93' => '升级押金'
//                '90'=> 'POS交易返佣',
//                '91'=> '流量返现（专项）',
//                '92'=> '交易返现（专项）',
//                '93'=> '升级押金'
    ];

    // 定义关联方法
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function getTypeNameAttr($value, $data)
    {
        return self::$show_type[$data['type']];
    }


    public function getChannelAttr($value, $data)
    {
        $value = '余额';
        if (!empty($data['modular_id'])) {
            $value = Modular::where('id', $data['modular_id'])->value('name');
        }

        if ($value == '余额' && !empty($data['order_id'])) {
            if (in_array($data['type'], [90, 91, 92, 93])) {
                $value = 'POS机';
            } elseif (strpos('Jccaapply', $data['order_id']) >= 0) {
                $value = '信用卡';
            } else {
                $product_type = Db::name('order')->where('order_no', $data['order_id'])->value('product_type');
                if ($product_type) {
                    switch ($product_type) {
                        case 1:
                            $value = '信用卡';
                            break;
                        case 2:
                            $value = '积分兑换';
                            break;
                        case 3:
                            $value = '贷款';
                            break;
                        case 4:
                            $value = '游戏专区';
                            break;
                        case 5:
                            $value = '自营接单';
                            break;
                        case 6:
                            $value = '手机接单';
                            break;
                    }
                }
            }
        }
        return $value;
    }


    public function getLogoAttr($value, $data)
    {

        $logo = [
            'ye.png',
            'xyk.png',
            'jfdh.png',
            'dk.png',
            'yxzq.png',
            'zyjd.png',
            'sjjd.png',
            'pos.png'
        ];

        $value = $logo[0];
        if (!empty($data['modular_id'])) {
            $value = Modular::where('id', $data['modular_id'])->value('local_image') . '.png';
        }
        return $value;
    }


    public function from()
    {
        return $this->belongsTo(User::class, 'from_id')->field('id,nickname,avatar');
    }

    public function getFromInfoAttr($value, $data)
    {
//        if ($data['type'] == 37) {
//
//            return ['nickname' => '游戏试玩'];
//        } else {
            return $this->belongsTo(User::class, 'from_id')->field('id,nickname,avatar');
//        }
    }


    /**
     * 写入修改
     * @param $data
     */
    public static function onBeforeInsert($data)
    {
        $data['status'] = 1;
        $data['ip'] = request()->ip();
//			$data['os'] = request()->server('HTTP_USER_AGENT');
        $data['param'] = '';
    }


    public function getCreateTimeAttr($value, $data)
    {
        return $value ? $value : date('Y-m-d H:i:s', $data['createtime']);
    }

}
